<?php

namespace App\Models\Club;

use Illuminate\Support\Facades\DB;
use App\Models\Model;
use App\Core\Utils\TwHelper\Random;
use App\Core\Utils\TwHelper\CommFunction;

class MemberModel extends Model
{
    const TABLENAME = 'tw_common_member';
    const TABLENAME_UCENTER_MEMBERS = 'tw_ucenter_members';
    const TABLENAME_MEMBER_PROFILE = 'tw_common_member_profile';
    const TABLENAME_MEMBER_STATUS = 'tw_common_member_status';
    const TABLENAME_MEMBER_COUNT = 'tw_common_member_count';
    const TABLENAME_MEMBER_FIELD_FORUM = 'tw_common_member_field_forum';
    const TABLENAME_MEMBER_FIELD_HOME = 'tw_common_member_field_home';
    const TABLENAME_COMMON_STAT = 'tw_common_stat';

    /**
     * 初始化并返回数据库请求构建器
     *
     * @return Illuminate\Support\Facades\DB::table(TABLENAME)
     */
    public static function dbTable()
    {
        return self::dbConnectionClub()->table(self::TABLENAME);
    }

    /**
     * 初始化并返回数据库请求构建器
     */
    public static function dbOtherTable($talbe)
    {
        // return self::dbConnectionClub()->table(self::TABLE_NAME);
        return self::dbConnectionClub()->table($talbe ? $talbe : self::TABLENAME);
    }

    /**
     * 注册bbs帐号
     */
    public static function signUp($mobile = '', $password = '', $username = '', $key = '')
    {
        $regdate = time();
        $email = strtolower(Random::getRandomPwd(6)) . '@' . $_SERVER ['HTTP_HOST'];
        $ip = CommFunction::getip();
        $udata = array();
        $udata ['username'] = $username;
        $udata ['password'] = $password;
        $udata ['email'] = $email;
        $udata ['regip'] = $ip;
        $udata ['regdate'] = $regdate;
        $udata ['salt'] = $key;
        $bbsid = self::addMemberToUcenterMember($udata);
        if ($bbsid) {
            $data = array();
            $data ['uid'] = $bbsid;
            $data ['username'] = $username;
            $data ['password'] = $password;
            $data ['mobilestatus'] = 1;
            $data ['groupid'] = 10;
            $data ['regdate'] = $regdate;
            $data ['email'] = $email;
            $data ['timeoffset'] = 9999;
            if (self::addMemberToCommonMember($data)) {
                $pdata = array();
                $pdata ['mobile'] = $mobile;
                $pdata ['uid'] = $bbsid;
                $pdata ['interest'] = $pdata ['bio'] = $pdata ['field1'] = $pdata ['field2'] = $pdata ['field3'] = $pdata ['field4'] = $pdata ['field5'] = $pdata ['field6'] = $pdata ['field7'] = $pdata ['field8'] = '';
                self::addMemberToMemberFile($pdata);
                //将信息写入其他表
                self::addOtherInfo($bbsid, $ip, $regdate);
            }
        }
        return $bbsid;
    }

    /**
     * 将信息写入其他表
     */
    public static function addOtherInfo($bbsid, $ip, $regdate)
    {
        $status = array(
            'uid' => $bbsid,
            'regip' => $ip,
            'lastip' => $ip,
            'lastvisit' => $regdate,
            'lastactivity' => $regdate,
            'lastpost' => 0,
            'lastsendmail' => 0
        );
        self::addMemberStatus($status);
        self::addMemberCount($bbsid);
        self::addMemberFieldForum($bbsid);
        self::addMemberFieldHome($bbsid);
        self::updateCommonStat();
    }

    /**
     * 向tw_ucenter_members表中注册用户
     *
     * @param string $password
     * @param string $username
     * @param string $key
     * @return mixed
     */
    public static function addMemberToUcenterMember($data)
    {
        return self::dbOtherTable(self::TABLENAME_UCENTER_MEMBERS)->insertGetId($data);
    }

    /**
     * 向tw_common_mebmer表添加用户信息
     */
    public static function addMemberToCommonMember($data)
    {
        return self::dbOtherTable(self::TABLENAME)->insertGetId($data);
    }

    /**
     * 向tw_common_mebmer表添加用户信息
     */
    public static function addMemberToMemberFile($data)
    {
        return self::dbOtherTable(self::TABLENAME_MEMBER_PROFILE)->insert($data);
    }

    /**
     * 向tw_common_member_status表添加用户信息
     */
    public static function addMemberStatus($data)
    {
        return self::dbOtherTable(self::TABLENAME_MEMBER_STATUS)->insert($data);
    }

    /**
     * 向tw_common_member_status表添加用户信息
     */
    public static function addMemberCount($bbsid)
    {
        $count = array(
            'uid' => $bbsid,
            'extcredits1' => 0,
            'extcredits2' => 0,
            'extcredits3' => 0,
            'extcredits4' => 0,
            'extcredits5' => 0,
            'extcredits6' => 0,
            'extcredits7' => 0,
            'extcredits8' => 0
        );
        return self::dbOtherTable(self::TABLENAME_MEMBER_COUNT)->insert($count);
    }

    /**
     * 向tw_common_member_field_forum表添加用户信息
     */
    public static function addMemberFieldForum($bbsid)
    {
        $ext = array(
            'uid' => $bbsid,
            'publishfeed' => 0,
            'customshow' => 26,
            'medals' => '',
            'sightml' => '',
            'groupterms' => '',
            'groups' => ''
        );
        return self::dbOtherTable(self::TABLENAME_MEMBER_FIELD_FORUM)->insert($ext);
    }

    /**
     * 向tw_common_member_field_home表添加用户信息
     */
    public static function addMemberFieldHome($bbsid)
    {
        $home = array(
            'uid' => $bbsid,
            'theme' => '',
            'spacecss' => '',
            'blockposition' => '',
            'recentnote' => '',
            'spacenote' => '',
            'privacy' => '',
            'feedfriend' => '',
            'acceptemail' => '',
            'magicgift' => '',
            'stickblogs' => ''
        );
        return self::dbOtherTable(self::TABLENAME_MEMBER_FIELD_HOME)->insert($home);
    }

    /**
     * 更新注册用户数量 tw_common_stat表的register字段
     */
    public static function updateCommonStat()
    {
        $time = date('Ymd', time());
        $where['daytime'] = $time;
        return self::dbOtherTable(self::TABLENAME_COMMON_STAT)->where($where)->increment('register');
    }

    public static function getUserByUid($id)
    {
        $where['uid'] = $id;
        return self::dbTable()->where($where)->first();
    }

    public static function getUserByUserName($username)
    {
        $where['username'] = $username;
        return self::dbTable()->where($where)->first();
    }

    public static function updateBbsUser($uid, $data = array())
    {
        $where['uid'] = $uid;
        return self::dbTable()->where($where)->update($data);
    }

    public static function getLastInsertId()
    {
        return self::dbTable()->select('uid')->orderBy('uid', 'desc')->first()['uid'];
    }

}

?>